/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html 
 *  
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

/**
 *
 * @author David Harrison
 * 3 May 2008
 * 
 */

import java.io.IOException;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.DontValidate;

import com.sfs.postcodes.dao.GeocodeLookupException;
import com.sfs.postcodes.dao.PostcodeLookupException;


@UrlBinding("/web/lookup")
public class LookupNewZealandActionBean extends LookupBaseActionBean {
            
    private String POSTCODE = "lookup-newzealand.jsp";     
    
    @DefaultHandler
    @DontValidate
    public Resolution welcome() {  
        Resolution response = new ForwardResolution(getView(POSTCODE));
        
        return response;
    }
    
    public Resolution search() {
        
        Resolution response = new ForwardResolution(getView(POSTCODE));
             
        try {
            setPostcode(getPostcodeDAO().findPostcode(getAddress(), 
                    "", "New Zealand"));
        } catch (PostcodeLookupException pe) {
            this.addMessage("Error performing postcode lookup: " 
                    + pe.getMessage());
        } 
             
        try {
            setGeocode(getGeocoderDAO().findGeocode(getAddress(), 
                    "", "New Zealand"));
        } catch (GeocodeLookupException ge) {
            this.addMessage("Error performing geocode lookup: " 
                    + ge.getMessage());
        } catch (IOException io) {
            this.addMessage("Error preparing for geocode lookup: "
                    + io.getMessage());
        }
        return response;
    }
    
}
